package com.jnshu.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.jnshu.constant.Constant;
import com.jnshu.dao.*;
import com.jnshu.dto.course.ContentDto;
import com.jnshu.dto.course.CourseList;
import com.jnshu.dto.course.VideoDto;
import com.jnshu.pojo.Course;
import com.jnshu.service.BackCourseService;
import com.jnshu.tools.HeadUtil;
import com.jnshu.tools.resultUtils.RUtil;
import com.jnshu.tools.resultUtils.ResultBean;
import com.jnshu.tools.resultUtils.ResultEnum;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BackCourseServiceImpl
 * @Descrintion
 * @Author bd
 * @Date 2019/8/3 15:05
 * @Version 1.0
 **/
@Log
@Service
public class BackCourseServiceImpl implements BackCourseService {

    @Autowired(required = false)
    CourseMapper courseMapper;
    @Autowired(required = false)
    CollectMapper collectMapper;
    @Autowired(required = false)
    HistoryMapper historyMapper;
    @Autowired(required = false)
    ViewMapper viewMapper;
    @Autowired(required = false)
    HeadUtil headUtil;
    @Autowired(required = false)
    ManageMapper manageMapper;
    @Autowired(required = false)
    RoleMapper roleMapper;

    /*
     * @Author bd
     * @Description 文章列表
     * @Date 14:28 2019/8/2
     * @Param [course]
     * @return java.util.List<com.jnshu.dto.course.CourseList>
     **/

    @Override
    public ResultBean aSelectList(Course course, int pageSize, int pageNum) {
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<CourseList> courses = courseMapper.aSelectList(course);
        log.info("数据为" + courses);
        PageInfo<CourseList> info = new PageInfo<>(courses);
        log.info("物理页数" + info.getTotal());
        List<CourseList> courses1 = new ArrayList<>();
        for (CourseList cn : info.getList()) {
            String roleName = roleMapper.selectRoleName(cn.getRoleId());
            cn.setRoleName(roleName);
            courses1.add(cn);
        }
        info.setList(courses1);
        log.info("物理页数" + info.getList());

        if (info == null) {
            return RUtil.error(401, "资料不存在");
        } else {
            return RUtil.ok(info);
        }
    }


    /*
     * @Author bd
     * @Description 上下架
     * @Date 14:30 2019/8/2
     * @Param [id, status]
     * @return boolean
     **/
    @Override
    public ResultBean aStatus(long id, int status) {
        log.info("上下架传入的数据是 id=" + id + ",status=" + status);
        if (status == 0) {
            status = 1;
        } else {
            status = 0;
        }
        Course course = new Course();
        course.setUpdateAt(System.currentTimeMillis());
        course.setId(id);
        course.setStatus(status);
        boolean rs = courseMapper.updateByPrimaryKeySelective(course);
        if (rs == true) {
            return RUtil.ok();
        } else {
            return RUtil.error(ResultEnum.FAILURE);
        }

    }

    /*
     * @Author bd
     * @Description 删除课程
     * @Date 14:31 2019/8/2
     * @Param [id]
     * @return boolean
     **/
    @Override
    public ResultBean deleteByPrimaryKey(long id) {
        log.info("要删除的课程的id是" + id);
        try {
            boolean rs = courseMapper.deleteByPrimaryKey(id);
            log.info("资料删除" + rs);
            boolean rs1 = collectMapper.deleteByPrimaryKey(id);
            log.info("收藏删除" + rs);
            boolean rs2 = historyMapper.deleteByPrimaryKey(id);
            log.info("历史记录删除" + rs);
            boolean rs3 = viewMapper.deleteByPrimaryKey(id);
            log.info("阅读量记录删除" + rs);
            if (rs == false) {
                return RUtil.error(401, "资料不存在");
            }
            return RUtil.ok();
        } catch (Exception e) {
            log.info("执行删除过程中是失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RUtil.error(401, "资料删除失败");
        }

    }

    /*
     * @Author bd
     * @Description 视频详情
     * @Date 14:31 2019/8/2
     * @Param [id]
     * @return com.jnshu.dto.course.VideoDto
     **/
    @Override
    public ResultBean aSelectVideo(Long id) {

        log.info("需要查询视频的id为" + id);
        Course course = courseMapper.aSelectVideo(id);
        VideoDto videoDto = new VideoDto();
        BeanUtils.copyProperties(course, videoDto);
        log.info("视频的详情为" + videoDto);
        if (videoDto == null) {
            return RUtil.error(401, "视频不存在");
        }
        return RUtil.ok(videoDto);

    }

    /*
     * @Author bd
     * @Description 新增视频
     * @Date 14:31 2019/8/2
     * @Param [course]
     * @return boolean
     **/
    @Override
    public ResultBean aInsertVideo(Course course) {
        log.info("service层新增视频为" + course);
        long id = Long.parseLong(headUtil.getHeadValue(Constant.LOGINID));
        long roleId = manageMapper.selectRole(id);

        course.setRoleId(roleId);
        course.setUpdateBy(id);
        course.setUpdateBy(id);
//        course.setClassify(1);

        course.setUpdateAt(System.currentTimeMillis());
        course.setCreateAt(System.currentTimeMillis());
        //获取用户名，根据用户名来获取用户的角色
        course.setStatus(1);
        boolean rs = courseMapper.aInsertVideo(course);
        log.info("新增视频结果为" + rs);
        if (rs == false) {
            return RUtil.error(ResultEnum.FAILURE);
        } else {
            return RUtil.ok();
        }

    }

    /*
     * @Author bd
     * @Description 修改视频
     * @Date 14:31 2019/8/2
     * @Param [course]
     * @return boolean
     **/
    @Override
    public ResultBean aUpdateVideo(Course course) {
        if (course.getClassify() != 1) {
            return RUtil.error(202, "该请求为视频修改");
        }

        long id = Long.parseLong(headUtil.getHeadValue(Constant.LOGINID));
        long roleId = manageMapper.selectRole(id);
        course.setRoleId(roleId);
        course.setUpdateBy(id);
        course.setClassify(1);
        course.setUpdateAt(System.currentTimeMillis());
        boolean rs = courseMapper.aUpdateVideo(course);
        log.info("修改视频结果为" + rs);
        if (rs == false) {
            return RUtil.error(ResultEnum.FAILURE);
        } else {
            return RUtil.ok();
        }
    }

    /*
     * @Author bd
     * @Description 文章详情
     * @Date 14:31 2019/8/2
     * @Param [id]
     * @return com.jnshu.dto.course.ContentDto
     **/
    @Override
    public ResultBean aSelectContent(Long id) {
        log.info("需要查询文章的id为" + id);
        Course course = courseMapper.aSelectContent(id);
        log.info("视频的详情为" + course);
        ContentDto contentDto = new ContentDto();
        BeanUtils.copyProperties(course, contentDto);

        if (contentDto == null) {
            return RUtil.error(401, "视频不存在");
        }
        return RUtil.ok(contentDto);
    }

    /*
     * @Author bd
     * @Description 新增文章
     * @Date 14:32 2019/8/2
     * @Param [course]
     * @return boolean
     **/
    @Override
    public ResultBean aInsertContent(Course course) {

        long id = Long.parseLong(headUtil.getHeadValue(Constant.LOGINID));
        long roleId = manageMapper.selectRole(id);
        course.setRoleId(roleId);
        course.setUpdateBy(id);
        course.setCreateBy(id);

        course.setClassify(0);
        course.setUpdateAt(System.currentTimeMillis());
        course.setCreateAt(System.currentTimeMillis());
        course.setStatus(1);
        boolean rs = courseMapper.aInsertContent(course);
        log.info("新增文章结果为" + rs);
        if (rs == false) {
            return RUtil.error(ResultEnum.FAILURE);
        } else {
            return RUtil.ok();
        }

    }

    /*
     * @Author bd
     * @Description 修改文章
     * @Date 14:32 2019/8/2
     * @Param [course]
     * @return boolean
     **/
    @Override
    public ResultBean aUpdateContent(Course course) {

        if (course.getClassify() != 0) {
            return RUtil.error(202, "该请求为文章修改");
        }
        long id = Long.parseLong(headUtil.getHeadValue(Constant.LOGINID));
        long roleId = manageMapper.selectRole(id);
        course.setRoleId(roleId);
        course.setUpdateBy(id);
        course.setClassify(0);
        course.setUpdateAt(System.currentTimeMillis());
        boolean rs = courseMapper.aUpdateContent(course);
        log.info("修改文章结果为" + rs);
        if (rs == false) {
            return RUtil.error(ResultEnum.FAILURE);
        } else {
            return RUtil.ok();
        }
    }

}
